'''
7.今日公交调度率仪表图
3.公交停靠站点渐变柱状图
'''
from pyecharts.charts import Bar
import mysql.connector
import pyecharts.options as opts
from pyecharts.charts import Gauge
from pyecharts.commons.utils import JsCode
from pyecharts.globals import ThemeType

# 创建连接
cnx = mysql.connector.connect(user='root', password='123456',
                              host='localhost', database='bus_view')

# 创建游标对象
cursor = cnx.cursor()

# SQL 查询语句
sql = "SELECT bus_num,station_count FROM bus_count order by station_count desc LIMIT 10"
cursor.execute(sql)
# 获取所有记录列表
results = cursor.fetchall()
add_xaxis = []
add_yaxis = []
for x, y in results:
    add_xaxis.append(x + '路')
    add_yaxis.append(y)

sql2 = "SELECT sum(bus_count) FROM bus_count"
cursor.execute(sql2)
todayCount = cursor.fetchall()
allCount = 1731
todayCounts = todayCount[0][0]
result = (todayCount[0][0] / allCount) * 100
print("开始绘制仪表盘图...")
# 仪表盘图
gauge = (
    Gauge(init_opts=opts.InitOpts(height="300px", width="400px", chart_id='gauge',
                                  bg_color="rgba(16, 12, 42,0.8)"))
    .add(series_name="近一周公交调度率",
         data_pair=[[("今日公交总车数：" + str(todayCounts))+'\n', int(result)]],
         axisline_opts=opts.AxisLineOpts(
             linestyle_opts=opts.LineStyleOpts(
                 color=[(0.3, "#67e0e3"), (0.7, "#37a2da"), (1, "#fd666d")], width=10
             )
         )
         )
    .set_global_opts(
        legend_opts=opts.LegendOpts(is_show=False),
        tooltip_opts=opts.TooltipOpts(is_show=True, formatter="{a} <br/>{b} : {c}%"),
        title_opts=opts.TitleOpts(title='今日公交调度率',
                                  title_textstyle_opts=opts.TextStyleOpts(
                                      font_size=15,
                                      # 标题的字体大小
                                      font_family='微软雅黑',
                                      color="#edf0f9"
                                  ),

                                  pos_left='center',
                                  pos_top='5px')

    )

)
gauge.render("7.仪表盘图.html")

# 渐变柱状图
cursor.close()
print("开始绘制渐变柱状图...")
bar3 = (
    Bar(opts.InitOpts(theme=ThemeType.DARK, height="300px", width="400px", chart_id='bar3',
                      bg_color="rgba(16, 12, 42,0.8)"))
    .add_xaxis(add_xaxis)
    .add_yaxis("站点总数", add_yaxis, category_gap="60%", )
    .set_series_opts(
        itemstyle_opts={
            "normal": {
                "color": JsCode(
                    """new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: 'rgba(0, 244, 255, 1)'
            }, {
                offset: 1,
                color: 'rgba(0, 77, 167, 1)'
            }], false)"""
                ),
                "barBorderRadius": [5, 5, 0, 0],
                "shadowColor": "rgb(0, 160, 221)",
            }
        }
    )
    .set_global_opts(legend_opts=opts.LegendOpts(pos_left="left"),
                     title_opts=opts.TitleOpts(title='公交停靠站点数',
                                               subtitle='Number of bus stops',
                                               title_textstyle_opts=opts.TextStyleOpts(
                                                   font_size=15,
                                                   # 标题的字体大小
                                                   font_family='微软雅黑',
                                               ),
                                               pos_left='center',
                                               pos_top='10px'))

)
bar3.render("3.渐变柱状图.html")
